package com.chuku.controller;

import com.alibaba.fastjson.JSON;
import com.bean.*;
import com.chuku.service.ChukuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@Controller
public class ChukuController {

    @Autowired
    ChukuService chukuService;

    @RequestMapping("getChukubanli.do")
    public String test(HttpServletRequest request)
    {
        String uuid = UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
        String uuidNew = uuid.substring(0,6);
        request.getSession().setAttribute("uuid",uuidNew);
        return "chuku/chuku_banli";
    }
    //出库办理
    @RequestMapping("getManyHuozhu.do")
    @ResponseBody
    public List<WmsTCustomer> getManyHuozhu(String custName,HttpServletRequest request)
    {
        System.out.println("前端获取到的数据:"+custName);
        List<WmsTCustomer> wmsTCustomers  = chukuService.getManyHuozhu(custName);
        System.out.println("模糊查询出的值:"+wmsTCustomers);
        request.getSession().setAttribute("wmsTCustomers",wmsTCustomers);
        return wmsTCustomers;
    }

    @RequestMapping("getManyMingxi.do")
    @ResponseBody
    public String getManyMingxi(String custId,HttpServletRequest request)
    {
        System.out.println("前端传过来的:custId:"+custId);
        List<WmsTBillDetails> wmsTBillDetails1 = chukuService.getManyMingxi(custId);
        System.out.println("明细:"+wmsTBillDetails1);
        String  wmsTBillDetails = JSON.toJSONString(wmsTBillDetails1);
        request.getSession().setAttribute("jiesuan","0");
        return wmsTBillDetails;
    }
    @RequestMapping("jiesuan.do")
    public void jiesuan(WmsTBill wmsTBill, HttpServletResponse response,HttpServletRequest request) throws IOException {
        System.out.println("前端回来的对象信息:"+wmsTBill);
        String customerId1 = wmsTBill.getCustomerId();
        String  customerId = customerId1.split("-")[0];
        wmsTBill.setCustomerId(customerId);
        wmsTBill.setBillType("1");
        wmsTBill.setBillState("0");
        String time = wmsTBill.getBillDate();
        wmsTBill.setBillEditTime(time);
        System.out.println("新的对象="+wmsTBill);
        WmsTUser wmsTUser = (WmsTUser) request.getSession().getAttribute("user");
        System.out.println("wmsTUserd的值:"+wmsTUser);
        String billTransactor = wmsTUser.getUserId();
        System.out.println("userId的值:"+billTransactor);
        wmsTBill.setBillTransactor(billTransactor);
        //往bill表中添加明细信息
        chukuService.addMingxi(wmsTBill);

        //插入billdetails表
        List<WmsTBillDetails> insert = chukuService.getManyMingxi(customerId);
        //根据trayId修改trayState
        for (WmsTBillDetails ad:insert) {
            String trayId = ad.getTrayId();
            chukuService.accordingTrayidByTraystate(trayId);
        }
        System.out.println("原始数据"+insert);
//        for (WmsTBillDetails ad:insert) {
//            //修改需要插入Details里所需要的数据
//            ad.setDetailsId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase().substring(0, 5));
//            ad.setBillId(wmsTBill.getBillId());
//            ad.setDetailsEditTime(wmsTBill.getBillEditTime());
//
//            chukuService.insert2(ad);//        }
        request.getSession().setAttribute("jiesun","1");
        response.sendRedirect("getChukubanli.do");
    }

    @RequestMapping("getManyCangkuName.do")
    public String getManyCangkuName(HttpServletRequest request)
    {
        List<WmsTStore> manyCangkuName = chukuService.getManyCangkuName();
        System.out.println("manyCangkuName"+manyCangkuName);
        request.getSession().setAttribute("manyCangkuName",manyCangkuName);
        return "chuku/chuku_danmingxi";
    }
    //查询仓库信息
    @RequestMapping("chaxuncangku.do")
    public String getManyXinxi(String pageRows,String storeId,String page,HttpServletRequest request)
    {
        System.out.println("前端获取过来的storeId:"+storeId);
        System.out.println("前端传过来的页码:"+page);
        int pageNum = 1;
        if (page!=null)
        {
            if(!page.equals(""))
            {
               pageNum = Integer.parseInt(page);
            }

        }
        int pageRows1 = 15;
        if (pageRows!=null)
        {
            pageRows1 = Integer.parseInt(pageRows);
        }
        System.out.println(pageRows1);
        List<Map<String,Object>> all1 = chukuService.getAll(storeId);
        System.out.println("ssssssss"+all1);
        PageHelper.startPage(pageNum,pageRows1);
        List<Map<String,Object>> all = chukuService.getAll(storeId);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(all);
//        pageInfo.setPageSize(Integer.parseInt(pageRows));
        System.out.println("pageInfo"+pageInfo);
        request.getSession().setAttribute("pageInfo",pageInfo);
        request.getSession().setAttribute("storeId",storeId);
        return "chuku/chuku_danmingxi";
    }
    //出库单查询
    @RequestMapping("chukudanChaxun.do")
    public String chukudanChaxun()
    {
        return "chuku/chuku_danchaxun";
    }
    @RequestMapping("chukudanChaxun1.do")
    public String chukudanChaxun1(String pageRows,String page,String billId,String custName,String time1,String time2,HttpServletRequest request)
    {
        System.out.println("前端传过来的页码:"+page);
        int pageNum = 1;
        if (page!=null)
        {
            if(!page.equals(""))
            {
                pageNum = Integer.parseInt(page);
            }
        }
        int pageRows1 = 15;
        if (pageRows!=null)
        {
            if(!pageRows.equals(""))
            {
                pageRows1 = Integer.parseInt(pageRows);
            }

        }
        System.out.println("第二次"+pageRows);
        List<Map<String,Object>> map1 = chukuService.selectChukujilu(billId,custName,time1,time2);
        System.out.println("map的值:"+map1);
        PageHelper.startPage(pageNum,pageRows1);
        List<Map<String,Object>> map = chukuService.selectChukujilu(billId,custName,time1,time2);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(map);
//        pageInfo.setPageSize(Integer.parseInt(pageRows));
        System.out.println("动态查询数据:"+map);
        request.getSession().setAttribute("pageInfo2",pageInfo);
//        request.getSession().setAttribute("mapChaxun",map);
        request.getSession().setAttribute("billId",billId);
        request.getSession().setAttribute("custName",custName);
        request.getSession().setAttribute("time1",time1);
        request.getSession().setAttribute("time2",time2);
        return "chuku/chuku_danchaxun";
    }

    //出库单明细查询
    @RequestMapping("chukudanmingxichaxun.do")
    public String chukudanmingxichaxun()
    {
        return "chuku/chuku_danmingxichaxun";
    }
    @RequestMapping("chukudanmingxichaxun1.do")
    public String chukudanmingxichaxun1(String pageRows,String page,String billId,String custName,String time1,String time2,HttpServletRequest request)
    {
        System.out.println("前端传过来的页码:"+page);
        int pageNum = 1;
        if (page!=null)
        {
            if(!page.equals(""))
            {
                pageNum = Integer.parseInt(page);
            }

        }
        int pageRows1 = 15;
        if (pageRows!=null)
        {
            pageRows1 = Integer.parseInt(pageRows);
        }
        System.out.println("每页显示条数："+pageRows1);
        List<Map<String,Object>> map1 = chukuService.selectChukumingxi(billId,custName,time1,time2);
        System.out.println("map"+map1);
        PageHelper.startPage(pageNum,pageRows1);
        List<Map<String,Object>> map = chukuService.selectChukumingxi(billId,custName,time1,time2);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(map);
//        pageInfo.setPageSize(Integer.parseInt(pageRows));
        System.out.println("动态查询数据:"+pageInfo.getList());
        request.getSession().setAttribute("pageInfo3",pageInfo);
//        request.getSession().setAttribute("mapChaxun",map);
        request.getSession().setAttribute("billId",billId);
        request.getSession().setAttribute("custName",custName);
        request.getSession().setAttribute("time1",time1);
        request.getSession().setAttribute("time2",time2);
        return "chuku/chuku_danmingxichaxun";
    }
}
